package com.minispider;

import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.HttpUnitOptions;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import com.minispider.util.Logger;

public class HttpUnitTest {

	public static void main(String[] args) {

        HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);
        HttpUnitOptions.setScriptingEnabled(false);
                
        try {
            // create the conversation object which will maintain state for us
            WebConversation wc = new WebConversation();

            for (int i=0; i < 1; i++) {
            	
        		Logger.debug( i + "########################################################################");
	            
        		WebRequest req = new GetMethodWebRequest("http://www.tianya.cn/publicforum/articleslist/0/water.shtml" );
        		
        		req.setHeaderField("user-agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; CIBA; .NET CLR 1.1.4322)");
        		req.setHeaderField("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/QVOD, application/QVOD, application/vnd.ms-excel, application/msword, */*");
        		req.setHeaderField("Accept-Encoding", "gzip, deflate");

        		WebResponse response = wc.getResponse(req);
	    		
	            Logger.debug( response.getText());
	            
	            String c[] = response.getNewCookieNames();
	            
	            Logger.debug(c.length + " && 222#######################################################################");

	            Thread.sleep(10000);
                WebLink httpunitLink = response.getFirstMatchingLink( WebLink.MATCH_URL_STRING, 
        		"http://www.tianya.cn/new/publicforum/articleslist.asp?pageNO=2&strItem=water&Part=0" );
                	   //http://www.tianya.cn/new/publicforum/articleslist.asp?pageNO=2&strItem=water&Part=0&NextArticle=2010%2D5%2D16+14%3A00%3A43&strSubItem=&strSubItem2=" );
                       //http://www.tianya.cn/new/publicforum/articleslist.asp?pageNO=2&strItem=water&Part=0&NextArticle=2010%2D5%2D16+14%3A15%3A16&strSubItem=&strSubItem2=
                       //  http://www.tianya.cn/new/publicforum/articleslist.asp?pageNO=2&strItem=water&Part=0&NextArticle=2010%2D5%2D16+14%3A34%3A04&strSubItem=&strSubItem2=

                Logger.debug("httpunitLink.getURLString()  ###" + httpunitLink.getURLString() );       

                req = httpunitLink.getRequest();
        		req.setHeaderField("user-agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; CIBA; .NET CLR 1.1.4322)");
        		req.setHeaderField("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/QVOD, application/QVOD, application/vnd.ms-excel, application/msword, */*");
        		req.setHeaderField("Accept-Encoding", "gzip, deflate");


        		//response = wc.getResponse(req);
                response = httpunitLink.click();
	            Logger.debug("4########################################################################");
	    		
	            Logger.debug( response.getText());

            }

          } catch (Exception e) {
             System.err.println( "Exception: " + e );
             e.printStackTrace();
          }

	}

}
